package boot.spring.controller;

import boot.spring.entity.*;
import boot.spring.service.ModuleService;
import boot.spring.service.RoleService;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/*
* 模块控制类
* */
@RestController
@RequestMapping("/RoleController")
public class RoleController {
    @Autowired
  private RoleService roleService;
    /*注入模块表的业务层*/
    @Autowired
    private ModuleService moduleService;

    Result r=new Result();

@PostMapping("/getRoleByUidCon")
  public List<Map<String, Object>> getRoleByUidCon(String userId) {
      //获取用户拥有的模块
      List<Map<String, Object>> rList = roleService.getRoleByModel(userId);

      return rList;
  }
@GetMapping("/RoleConAllRole")
    public List<Role> RoleConAllRole(String userId) {
        //获取用户拥有的角色
        List<Role> rList = roleService.getRoleByUid(userId);

        return rList;
    }

  @GetMapping("/RoleConAll")/*获取所有的角色*/
  public Result RoleConAll(String token) {
      //获取所有角色
      return roleService.RoleMapAll();
  }

    @PostMapping("/RoleConAdd")/*新增角色*/
    public Result RoleConAdd(Role add) {
        return roleService.RoleSerAdd(add);
    }

    @GetMapping("/RoleConrepeatAll")/*根据角色名称查询重复的数据*/
    public Result RoleConrepeatAll(Role all) {
        //获取所有角色
        return roleService.RoleSerrepeatAll(all);
    }

    @PostMapping ("/RoleConUp")/*编辑角色信息*/
    public Result RoleConUp(Role up) {
        //获取所有角色
        return roleService.RoleSerUp(up);
    }

    @GetMapping("/ModuleConAll")/*获取所有的模块*/
    public List<Map<String,Object>> ModuleConAll() {
        return moduleService.ModuleSerAll();
    }

    @GetMapping("/getModuleByRidCon")/*根据角色id查询模块*/
    public List<Module> getModuleByRidCon(String roleId) {
        return moduleService.getModuleByRid(roleId);
    }

    @PostMapping("/RoleModuleConoperation")/*给角色分配模块*/
    public Result RoleModuleConoperation(String parentIds, String rId){
        return moduleService.RoleModuleSeroperation(parentIds,rId);
    }

    @PostMapping("/RoleConDel")/*删除角色信息*/
    public Result RoleConDel(Role del){
        return roleService.RoleSerDel(del);
    }

    @RequestMapping("/ListPmByidConAll")/*根据角色id查询出权限、模块、进行融合返回*/
    public Result ListPmByidConAll(String roleid){
        List<PermissionsModule> permissionsModules = roleService.ListPmByidSerAll(roleid);
        if(permissionsModules.size()>0){
            r=new Result(0,"请求成功",permissionsModules.size(),permissionsModules);
        }else{
            r=new Result(1,"请求失败");
        }
        return r;
    }

    /*根据模块名称和角色id查询角色权限表里所没有的权限*/
    @GetMapping("/ListModuleRoleIdConAll")
    public Result ListModuleRoleIdConAll(String mname, String roleid) {
        return roleService.ListModuleRoleIdSerAll(mname,roleid);
    }

    @PostMapping("/GetRperConAdd")/*向角色权限关联表里添加数据*/
    public Result GetRperConAdd(RolePermission add) {
        return roleService.GetRperSerAdd(add);
    }

    @PostMapping("/GetRperConDel")/*根据角色id和权限id删除角色权限关联表里的数据*/
    public Result GetRperConDel(RolePermission del) {
        return roleService.GetRperSerDel(del);
    }
}
